package edu.bailie.springmvc.controller;


import edu.bailie.springmvc.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;


@Controller
public class HelloController {


	@GetMapping("/hello1")
	public String hello1(){
		System.out.println("正在处理请求....");
		// 返回逻辑视图名称（决定跳转到哪个页面）
		//TODO:
		return "first";
	}

	@RequestMapping(value={"/hello2","/helloworld2"},method = RequestMethod.POST,params = {"username","!password"})
//	@PostMapping("/hello2")
//	@GetMapping("/hello")
//	@RequestParam()
	public String hello2(){
		System.out.println("正在处理请求....");
		// 返回逻辑视图名称（决定跳转到哪个页面）
		//TODO:
		return "first";
	}

	@RequestMapping(value={"/hello3","/helloworld3"})

	public String hello3(HttpServletRequest request){
		String uname = request.getParameter("username");
		//TODO:
		String pwd = request.getParameter("password");

		System.out.println(uname+"    "+pwd);
		// 返回逻辑视图名称（决定跳转到哪个页面）
		return "first";
	}



	@RequestMapping(value={"/hello4","/helloworld4"})

	public String hello4(
		@RequestParam(value = "username")
		String uname,
		@RequestParam(value = "password",defaultValue = "111111")
		String pwd
	){
//TODO:

		System.out.println(uname+"    "+pwd);
		// 返回逻辑视图名称（决定跳转到哪个页面）
		return "first";
	}

	@RequestMapping(value={"/hello5","/helloworld5"})

	public String hello5(User user){
		String un = user.getUsername();
		String pd = user.getPassword();
//TODO:
		System.out.println(user);
		System.out.println(un+"    "+pd);
		// 返回逻辑视图名称（决定跳转到哪个页面）
		return "first";
	}

	@RequestMapping(value={"/hello6","/helloworld6"})

	public String hello6(HttpServletRequest request){

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(username);
		System.out.println(password);
		// 返回逻辑视图名称（决定跳转到哪个页面）
		return "first";
	}

	@RequestMapping(value={"/hello7","/helloworld7"})
//TODO:
	public String hello7(
		@RequestParam(value = "username")String uname,
		@RequestParam(value = "password")String pwd
	){

		System.out.println(uname);
		System.out.println(pwd);
		// 返回逻辑视图名称（决定跳转到哪个页面）
		return "first";
	}

	@RequestMapping(value={"/hello8","/helloworld8"})

	public String hello8(User user){

		String username = user.getUsername();
		String password = user.getPassword();
		System.out.println(username);
		System.out.println(password);
		// 返回逻辑视图名称（决定跳转到哪个页面）
		return "first";
	}


	/**
	 *返回页面上  index
	 * @return ModelAndView
	 */
	@RequestMapping("/hellotoday")
	public ModelAndView hellotoday(){

		ModelAndView modelAndView = new ModelAndView();
		//返回数据//TODO:
		modelAndView.addObject("user","张学通");
		//返回视图 页面
		modelAndView.setViewName("index");
		return modelAndView;
	}


	@RequestMapping("/hellotorm")
	public String hellotoday(Model model){

		model.addAttribute("user","张学通");
		return "index";
	}


	@RequestMapping("/hellotoday1")
	public ModelAndView hellotoday1(){
//TODO:
		ModelAndView modelAndView = new ModelAndView();


		Map<String,String> map = new HashMap<>();
		map.put("user1","zhangxuetong");
		map.put("user2","lining");
		//返回数据
		modelAndView.addAllObjects(map);
		//返回视图 页面
		modelAndView.setViewName("index");
		return modelAndView;
	}


	@RequestMapping("/echo")
	public ModelAndView echo(){
		User user = new User();
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("index");
//		modelAndView.addObject("user","王震");
		user.setUsername("王震");
		user.setPassword("wangzhen123");
		modelAndView.addObject(user);
		return modelAndView;
	}

	@RequestMapping("/echo3")
	public ModelAndView echo3(){

		ModelAndView modelAndView = new ModelAndView();
//		视图的路径  页面
		modelAndView.setViewName("index");
//		数据
		modelAndView.addObject("user","白爽");
		return modelAndView;
	}


}
